// pages/pensionServices/pensionServices.js
const app = getApp();
var util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor:app.globalData.themeColor,
    pensionList:[
        {
            img:'/resources/pensionService/pension-icon01.png',
            text:'电子围栏',
            link:'/subPages/pensionService/electronicFence/electronicFence'
        },
        {
            img:'/resources/pensionService/pension-icon02.png',
            text:'一键呼救',
            link:'makecall'
        },
        {
            img:'/resources/pensionService/pension-icon03.png',
            text:'智能居家监护',
            link:'/subPages/pensionService/homeCare/homeCare'
        },
        {
            img:'/resources/pensionService/pension-icon04.png',
            text:'健康数据',
            link:'/subPages/health/healthData/index/index'
        },
    ],
    dayCareList:[
        {
          img:'/resources/pensionService/pension-icon05.png',
          text:'服务上门',
          link:'/subPages/pensionService/serviceDoor/serviceDoor'
        },
        {
            img:'/resources/pensionService/pension-icon06.png',
            text:'疾病配餐',
            link:"/subPages/pensionService/newKitchen/newKitchen?id=2"
        }
    ],
    recoveryList:[
        {
            img:'/resources/pensionService/pension-icon07.png',
            text:'康复教育',
            link: "/subPages/pensionService/rehabilitatioEducation/rehabilitationEducation?type=1"

        },
        {
            img:'/resources/pensionService/pension-icon08.png',
            text:'线上指导',
            link:'/subPages/pensionService/rehabilitatioEducation/rehabilitationEducation?type=2'
        }
    ]
  },
  toNextPage(e){
      let type = e.currentTarget.dataset.type;
      // type 1养老    2日间照料  3康复教育

      let index = e.currentTarget.dataset.index;
      let list,
          _this = this;
      // console.log(type,index);
      if(type == 1){
          list = _this.data.pensionList
      }else if(type == 2){
          list = _this.data.dayCareList
      }else if(type == 3){
          list = _this.data.recoveryList
      }
      if(list[index].link == 'makecall'){
          wx.makePhoneCall({
              phoneNumber:'120'
          })
      }else if(list[index].text == '健康数据'){
        if(app.globalData.userId){
            _this.navigateTo(list[index].link)
        }else{
            util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
                if (app.globalData.userInfo) {
                //已授权
                wx.redirectTo({
                    url: '/pages/login/login'
                })
                }else{
                    // 在没有 open-type=getUserInfo 版本的兼容处理
                    if(app.globalData.isAuthorized){
                        app.userInfoReadyCallback = res => {
                            if (res) {
                                //已授权
                                wx.redirectTo({
                                    url: '/pages/login/login'
                                })
                            }else{
                                //未授权
                                wx.navigateTo({
                                    url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                                })
                            }
                        }
                    }else{
                        wx.navigateTo({
                            url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                        })
                    }
                }
            })
        }
      }else{
        _this.navigateTo(list[index].link)
    }
  },
   navigateTo(link){
      wx.navigateTo({
          url:link
      })
   },
    //详情
    toIntro(e){
        var that = this
        let index_ = e.currentTarget.dataset.index_;
        wx.navigateTo({
            url:'/subPages/pensionService/pensionIntro/pensionIntro?index_=' + index_
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    util.setNavBarColor('#ffffff',app.globalData.themeColor);
    util.setNavBarTitle('养老服务');
    if(app.globalData.communityId){
        that.setData({
            communityId: app.globalData.communityId
        });
        that.getInfo();
    }else{
        app.communityIdCallback = communityId => {
            if (communityId != '') {
                that.setData({
                    communityId: communityId
                });
                that.getInfo();
            }
        }
    }
  },
  //获取所有
  getInfo(){
      let that = this;
      util.getAjax(`${app.globalData.apiUrl}/Api/Admincommunity/get_yl_content`,res=>{
          console.log(res);
          that.setData({
              pensionInfo:res.data
          })
      },{
          community_id:that.data.communityId
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if(that.data.communityId){
        that.getInfo();
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
